﻿using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using wms_wpf.Apiservice;
using wms_wpf.Dto;
using wms_wpf.InPut;
using wms_wpf.Views;

namespace wms_wpf.ViewModels
{
    public class HouseConfigViewModel:BindableBase
    {
        public HouseConfigViewModel(HouseConfigView view)
        {
            GetList_WarehouseLineCommand = new DelegateCommand(GetList_WarehouseLine);
            PageUpdatedCmd = new DelegateCommand(GetList_WarehouseLine);
            EnableCommand = new DelegateCommand(Enable);
            DisableCommand = new DelegateCommand(Disable);
            service = new BaseService();
            WarehouseLineDtos = new List<WarehouseLineDto>();
            InPut_BaseService = new InPut_BaseService4();
            this.service = service;
            InPut_BaseService.PageIndex = 1;
            InPut_BaseService.PageSize = 20;
            this.View = view;
            GetList_WarehouseLine();
        }

        public DelegateCommand GetList_WarehouseLineCommand { get; set; }
        public DelegateCommand PageUpdatedCmd { get; set; }
        public DelegateCommand EnableCommand { get; set; }
        public DelegateCommand DisableCommand { get; set; }

        private HouseConfigView View { get; set; }
        private IBaseService service { get; set; }
        private List<WarehouseLineDto> warehouseLineDtos;
        public List<WarehouseLineDto> WarehouseLineDtos
        {
            get { return warehouseLineDtos; }
            set { warehouseLineDtos = value; RaisePropertyChanged(); }
        }
        private InPut_BaseService4 inPut_BaseService;
        public InPut_BaseService4 InPut_BaseService
        {
            get { return inPut_BaseService; }
            set { inPut_BaseService = value;RaisePropertyChanged(); }
        }

        private long count;
        public long Count
        {
            get { return count; }
            set { count = value;RaisePropertyChanged(); }
        }
        private long pagecount;
        public long PageCount
        {
            get { return pagecount; }
            set { pagecount = value;RaisePropertyChanged(); }
        }




        private async void Enable()//启用
        {
            InPut_BaseService5 inPut=new InPut_BaseService5();
            if (View.datagrid.SelectedItems.Count == 0)
            {
                return;
            }
            foreach (WarehouseLineDto item in View.datagrid.SelectedItems)
            {
                inPut.WarehouseLineCodes.Add(item.WarehouseLineCode);
               
            }
            inPut.Type = true;
            bool res = await service.Update_WarehouseLines(inPut);
            if (res)
            {
                GetList_WarehouseLine();
            }
        }
        private async void Disable()//禁用
        {
            InPut_BaseService5 inPut = new InPut_BaseService5();
            if (View.datagrid.SelectedItems.Count == 0)
            {
                return;
            }
            foreach (WarehouseLineDto item in View.datagrid.SelectedItems)
            {
                inPut.WarehouseLineCodes.Add(item.WarehouseLineCode);
            }
            inPut.Type = false;
            bool res=   await  service.Update_WarehouseLines(inPut);
            if (res)
            {
                GetList_WarehouseLine();
            }
        }  
        private async void GetList_WarehouseLine()//获取数据
        {

       
            Tuple < List<WarehouseLineDto>,long?,long?> tuple = await service.GetList_WarehouseLine(InPut_BaseService);
            if (tuple == null)
            {
                return;
            }
            WarehouseLineDtos = tuple.Item1;          
            Count = (long)tuple.Item2;
            PageCount = (long)tuple.Item3;
       
        }





    }
}
